﻿using EggLink.DanhengServer.Util;
using Newtonsoft.Json;

namespace EggLink.DanhengServer.Database.Account;

public static class AccountHelper
{
    public static void CreateAccount (string username, string password, int uid = 0)
    {
        if (AccountData.GetAccountByUserName(username) != null)
            throw new Exception("Account already exists");

        var newUid = uid == 0 ? GenerateNewUid() : uid;

        Console.WriteLine($"接收注册/登录密码数据：{password}");
    

        var account = new AccountData
        {
            Uid = newUid,
            Username = username,
            Password = password,  // 明文存储
            Permissions = string.Join(",", ConfigManager.Config.ServerOption.DefaultPermissions)
        };

        DatabaseHelper.SaveInstance(account);
    }

    private static int GenerateNewUid ()
    {
        int uid = 10001;
        while (AccountData.GetAccountByUid(uid) != null) uid++;
        return uid;
    }
}